{extend name='main'}

{block name="content"}

<div class="think-box-shadow">

<table class="layui-table margin-top-10" lay-skin="line" id="dataTable"></table>

</div>

<script type="text/html" id="buttonTpl">
    <!-- <div class="layui-btn-group"> -->
        <!-- <a data-href="{:url('optimize')}?id={{ d.Name }}" class="layui-btn layui-btn-primary layui-btn-xs ">优化</a> -->
		<button data-load='{:url('optimize')}?id={{ d.Name }}' class='layui-btn layui-btn-xs layui-btn-primary'>优化</button>
		
		<button data-load='{:url('repair')}?id={{ d.Name }}' class='layui-btn layui-btn-xs layui-btn-primary'>修复</button>
    <!-- </div> -->
</script>

<script type="text/html" id="toolbar">
    <div class="layui-btn-group fl">
     <!-- <a data-href="{:url('export')}" class="layui-btn layui-btn-primary layui-btn-sm j-page-btns" data-table="dataTable">备份数据库</a> -->
     <!-- <button data-href="{:url('optimize')}?" class="layui-btn layui-btn-primary layui-btn-sm j-page-btns" data-table="dataTable">优化数据库1</button> -->
	 <!-- <button data-href="{:url('repair')}?" class="layui-btn layui-btn-primary layui-btn-sm j-page-btns" data-table="dataTable">修复数据库</button> -->

	
	 <button data-hrefs="{:url('export')}?" class="layui-btn layui-btn-primary layui-btn-sm" data-type="Backup">备份数据库</button>
	 <button data-hrefs="{:url('optimize')}?" class="layui-btn layui-btn-primary layui-btn-sm" data-type="Optimize">优化数据库</button>
	 <button data-hrefs="{:url('repair')}?" class="layui-btn layui-btn-primary layui-btn-sm" data-type="Repair">修复数据库</button>
	 
    </div>
</script>

<script type="text/javascript">
    layui.use(['table'], function() {
        var table = layui.table;
        table.render({
            elem: '#dataTable'
            ,url: '{:url('datalist')}?group=export' //数据接口
            ,page: false //分页
            ,skin: 'row'
            ,even: true
            ,text: {
                none : '暂无相关数据'
            }
            ,toolbar: '#toolbar'
            ,defaultToolbar: ['filter']
            ,cols: [[ //表头
                {type:'checkbox'}
                ,{field: 'Name', title: '表名'}
                ,{field: 'Rows', title: '数据量', width: 150}
                ,{field: 'Data_length', title: '大小', width: 150, templet: function(d) {
                    return d.Data_length / 1024;
                }}
                ,{field: 'Data_free', title: '冗余', width: 150, templet: function(d) {
                    return d.Data_free / 1024;
                }}
                ,{field: 'Comment', title: '备注'}
                ,{title: '操作', width: 200, templet: '#buttonTpl'}
            ]]
        });
		
		var query ='';
		  var $ = layui.$, active = {
			Optimize: function(){ //获取选中数据
			  var that = $(this);
			  var href = that.attr('href') ? that.attr('href') : that.attr('data-hrefs');
			  var checkStatus = table.checkStatus('dataTable')
			  ,data = checkStatus.data;
				if ($('.checkbox-ids:checked').length <= 0) {
					if (checkStatus.data.length <= 0) {
						layer.msg('请选择要操作的数据');
						return false;
					}
					for (var i in checkStatus.data) {
						if (i > 0) {
							query += '&';
						}
						query += 'id[]='+data[i].id;
					}
					//console.log(query);
				}
				layer.msg('数据优化中...',{time:10000});
                $.post(href, query, function(res) {
					//console.log(res);
					
                    layer.msg(res.info, {}, function(){
                        if (res.code != 0) {
                            location.reload();
                        } 
                    });
					
                });
				
				//layer.msg('选中了：'+ data.length + ' 个');
			  //layer.alert(JSON.stringify(data['id']));
			}
			,Repair: function(){ //获取选中数目
			  var that = $(this);
			  var href = that.attr('href') ? that.attr('href') : that.attr('data-hrefs');
			  
			  var checkStatus = table.checkStatus('dataTable')
			  ,data = checkStatus.data;
			  
				if ($('.checkbox-ids:checked').length <= 0) {
					if (checkStatus.data.length <= 0) {
						layer.msg('请选择要操作的数据');
						return false;
					}
					for (var i in checkStatus.data) {
						if (i > 0) {
							query += '&';
						}
						query += 'id[]='+data[i].id;
					}
					//console.log(query);
				}
			  
				layer.msg('数据修复中...',{time:10000});
                $.post(href, query, function(res) {
					//console.log(res);
					
                    layer.msg(res.info, {}, function(){
                        if (res.code != 0) {
                           location.reload();
                        } 
                    });
					
                });
			  //var checkStatus = table.checkStatus('dataTable')
			  //,data = checkStatus.data;
			  //layer.msg('选中了：'+ data.length + ' 个');
			}
			,Backup: function(){ //备份
			
			  var that = $(this);
			  var href = that.attr('href') ? that.attr('href') : that.attr('data-hrefs');
			  var checkStatus = table.checkStatus('dataTable')
			  ,data = checkStatus.data;
				if ($('.checkbox-ids:checked').length <= 0) {
					if (checkStatus.data.length <= 0) {
						layer.msg('请选择要操作的数据');
						return false;
					}
					for (var i in checkStatus.data) {
						if (i > 0) {
							query += '&';
						}
						query += 'id[]='+data[i].id;
					}
					//console.log(query);
				}
				layer.msg('正在备份中...',{time:10000});
                $.post(href, query, function(res) {
					console.log(res);
					
                    layer.msg(res.info, {}, function(){
                        if (res.code != 0) {
                            location.reload();
                        } 
                    });
					
                });
			
			
			
			
			  //var checkStatus = table.checkStatus('dataTable');
			  //layer.msg(checkStatus.isAll ? '全选': '未全选')
			}
		  };
		
		  $('.fl .layui-btn').on('click', function(){
			var type = $(this).data('type');
			active[type] ? active[type].call(this) : '';
		  });
		
		
    });
	

layui.define(['element', 'form', 'table'], function(exports) {
    var $ = layui.jquery,element = layui.element, 
        layer = layui.layer, 
        form = layui.form, 
        table = layui.table;

    var checkBrowser = function() {
        var d = layui.device();
        d.ie && d.ie < 10 && layer.alert("IE" + d.ie + "下体验不佳，推荐使用：Chrome/Firefox/Edge/极速模式");
    }
    checkBrowser();
	
    /**
     * 列表页批量操作按钮组
     * @attr href 操作地址
     * @attr data-table table容器ID
     * @class confirm 类似系统confirm
     * @attr tips confirm提示内容
     */
    $(document).on('click', '.j-page-btns,.fssOA-page-btns,.fssOA-table-ajax', function(){
		
        var that = $(this);
            var query='';
            var code = function(that) {
                var href = that.attr('href') ? that.attr('href') : that.attr('data-href');
                var tableObj = that.attr('data-table') ? that.attr('data-table') : 'dataTable';
                if (!href) {
                    layer.msg('请设置data-href参数');
                    return false;
                }

                if ($('.checkbox-ids:checked').length <= 0) {
                    var checkStatus = table.checkStatus(tableObj);
                    if (checkStatus.data.length <= 0) {
                        layer.msg('请选择要操作的数据');
                        return false;
                    }
                    for (var i in checkStatus.data) {
                        if (i > 0) {
                            query += '&';
                        }
                        query += 'id[]='+checkStatus.data[i].id;
                    }
                } else {
                    if (that.parents('form')[0]) {
                        query = that.parents('form').serialize();
                    } else {
                        query = $('#pageListForm').serialize();
                    }
                }

				layer.msg('数据提交中...',{time:10000});
				/*
				$.ajax({
					type: "get",
					url: "{:url('database/optimize')}?",
					data: query,
					dataType: "html",
					success: function(res){
						layer.msg(res.msg, {}, function(){
							if (res.code != 0) {
								location.reload();
							} 
						});
					}
				});
				*/
				
                $.post(href, query, function(res) {
					console.log(res);
					return false;
					/*
                    layer.msg(res.info, {}, function(){
                        if (res.code != 0) {
                           location.reload();
                        } 
                    });
					*/
                });
				
            };
        if (that.hasClass('confirm')) {
            var tips = that.attr('tips') ? that.attr('tips') : '您确定要执行此操作吗？';
            layer.confirm(tips, {title:false, closeBtn:0}, function(index){
                //code(that);
                layer.close(index);
            });
        } else {
           //code(that); 
        }
        return false;
    });
	
});
</script>
{/block}